# リファクタリング後
class Game

  def initialize
    @rolls = []
  end

  def roll(pins)
    @rolls << pins
  end

  def score
    roll_idx = 0
    score = 0
    
    10.times do |frame|
      if strike?(roll_idx)
        # strike
        score += strike_bounus(roll_idx)
        roll_idx += 1
      elsif spare?(roll_idx)
        # spare
        score += spare_bounus(roll_idx)
        roll_idx += 2
      else
        score += score_of_frame(roll_idx)
        roll_idx += 2
      end
    end
    
    score
  end

  private
  
  def strike?(roll_idx)
    @rolls[roll_idx] == 10
  end

  def spare?(roll_idx)
    @rolls[roll_idx] + @rolls[roll_idx + 1] == 10
  end

  def score_of_frame(roll_idx)
    @rolls[roll_idx] + @rolls[roll_idx + 1]
  end

  def strike_bounus(roll_idx)
    # 次の2投目までボーナス加算
    10 + @rolls[roll_idx + 1] + @rolls[roll_idx + 2]
  end

  def spare_bounus(roll_idx)
    # 次の1投をボーナス加算
    10 + @rolls[roll_idx + 2]
  end
end